package com.siun.dk.module;

import com.aw.baselib.base.AwBaseBindingActivity;
import com.sirun.ble.manager.BleManager;
import com.sirun.ble.utils.AwDataUtil;
import com.siun.dk.constants.MyConstant;
import com.siun.dk.events.EventRefreshBleNameType;
import com.siun.wuling.databinding.ActivitySettingBinding;

public class SettingActivity extends AwBaseBindingActivity<ActivitySettingBinding> {

    @Override
    protected ActivitySettingBinding getViewBinding() {
        return ActivitySettingBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
        super.initView();
        setToolbar("设置", null);
    }

    @Override
    protected void initData() {
        super.initData();
        setTextNoDefault(binding.etBleName, BleManager.getInstance().getBleName());
        setTextNoDefault(binding.etHost, MyConstant.BASE_URL);
    }

    @Override
    protected void initListener() {
        super.initListener();
        binding.tvSaveBleName.setOnClickListener(v -> {
            if(AwDataUtil.isEmpty(getText(binding.etBleName))) {
                showMsg("请输入蓝牙名称");
                return;
            }
            if(getText(binding.etBleName).equals(BleManager.getInstance().getBleName())) {
                showMsg("蓝牙名称未修改，无需保存");
                return;
            }
            BleManager.getInstance().setBleName(getText(binding.etBleName));
            postEvent(new EventRefreshBleNameType(null));
            finish();
        });
        binding.tvSaveHost.setOnClickListener(v -> {
            if(AwDataUtil.isEmpty(getText(binding.etHost))) {
                showMsg("请输入服务器地址");
                return;
            }
            if(!getText(binding.etHost).startsWith("http")) {
                showMsg("服务器地址格式错误");
                return;
            }
            if(getText(binding.etHost).equals(MyConstant.BASE_URL)) {
                showMsg("服务器地址未修改，无需保存");
                return;
            }
            MyConstant.BASE_URL = getText(binding.etHost);
            finish();
        });
    }
}
